package com.peach.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.peach.pojo.Activity;
import com.peach.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private ActivityService activityService;

    @GetMapping("/selectAllActivities2")
    public String selectAllActivities2(Model model,
                                       String activityAuthor,
                                       String activityContent,
                                       String user,
                                       @RequestParam(required = false,defaultValue="1",value="pageNum")Integer pageNum,
                                       @RequestParam(defaultValue="5",value="pageSize")Integer pageSize){

        System.out.println("?"+user);

        //为了程序的严谨性，判断非空：
        if(pageNum == null){
            //设置默认当前页
            pageNum = 1;
        }
        if(pageNum <= 0){
            pageNum = 1;
        }
        if(pageSize == null){
            //设置默认每页显示的数据数
            pageSize = 5;
        }

        PageHelper.startPage(pageNum,pageSize);

        try {

            List<Activity> activities = activityService.selectAllActivities(activityAuthor,activityContent);


            PageInfo<Activity> pageInfo = new PageInfo<Activity>(activities,pageSize);
            model.addAttribute("pageInfo",pageInfo);
            model.addAttribute("activityAuthor",activityAuthor);
            model.addAttribute("activityContent",activityContent);


        }finally {
            PageHelper.clearPage(); //清理 ThreadLocal 存储的分页参数,保证线程安全
        }
        return "activity_user";
    }

    @GetMapping("/selectAllActivities3")
    public String selectAllActivities3(Model model,
                                       String activityAuthor,
                                       String activityContent,
                                       @RequestParam(required = false,defaultValue="1",value="pageNum")Integer pageNum,
                                       @RequestParam(defaultValue="5",value="pageSize")Integer pageSize){
        //为了程序的严谨性，判断非空：
        if(pageNum == null){
            //设置默认当前页
            pageNum = 1;
        }
        if(pageNum <= 0){
            pageNum = 1;
        }
        if(pageSize == null){
            //设置默认每页显示的数据数
            pageSize = 5;
        }

        PageHelper.startPage(pageNum,pageSize);

        try {

            List<Activity> activities = activityService.selectAllActivities(activityAuthor,activityContent);

            PageInfo<Activity> pageInfo = new PageInfo<Activity>(activities,pageSize);
            model.addAttribute("pageInfo",pageInfo);
            model.addAttribute("activityAuthor",activityAuthor);
            model.addAttribute("activityContent",activityContent);


        }finally {
            PageHelper.clearPage(); //清理 ThreadLocal 存储的分页参数,保证线程安全
        }
        return "activity_teacher";
    }



}
